// index.js
import api from '../../utils/api'
import {
  submitTitle
} from '../../utils/env';
import {
  isWithin24Hours
} from '../../utils/util'
Page({
  data: {
    sexList: [{
      value: 1,
      name: '男'
    }, {
      value: 2,
      name: '女'
    }],
    colorList: [],
    carList: [],
    treeData: [],
    dataForm: {
      // operator: '13645557777',
      userCode: '',
      inviteCode: '',
      carNo: '',
      carColor: '',
      carColorName: '',
      authType: 1,
      carType: '',
      carTypeName: '',
      forwardUnit: '',
      // forwardPhone: '',
      field4: '',
      // receivePhone: '',
      inValidStime: '',
      outValidEtime: '',
      allowImgList: '2#',
      allowOmgList: '2#',
      carDriverName: '',
      carDriverPhone: '',
      receiveUnit: '',
      // field4: '',
      persons: [
        //   {
        //   personName: '',
        //   idCard: '',
        //   sexC: '',
        //   telNum: '',
        // }
      ],
      inFactTask: '',
      remark: '',
      file: '',
      billMdetails: [],
      isSubmitting: false
    }
  },
  onLoad() {
    api.getDictData({
      dictCode: 'car_color'
    }).then(res => {
      // this.data.dataList = res.data.records
      let arr = []
      res.data.forEach(item => {
        arr.push({
          name: item.dictName,
          value: item.dictValue
        })
      });
      this.setData({
        colorList: arr,
      });
    })
    api.getDictData({
      dictCode: 'PASSENGER_CAR_TYPE'
    }).then(res => {
      // this.data.dataList = res.data.records
      let arr = []
      res.data.forEach(item => {
        arr.push({
          name: item.dictName,
          value: item.dictValue
        })
      });
      this.setData({
        carList: arr,
      });
    })
    api.orgTreeTree({
      orgLevel: '3'
    }).then(res => {

      // this.data.dataList = res.data.records
      let arr = []
      arr = this.flattenTree(res.data)
      arr.forEach(item => {
        item.value = item.id
      });
      console.log(arr, 666)
      this.setData({
        treeData: arr,
      });
    })
  },
  flattenTree(treeData) {
    const result = [];
    const flatten = (node) => {
      result.push(node);
      if (node.childNode) {
        for (const child of node.childNode) {
          flatten(child);
        }
      }
    }
    for (const node of treeData) {
      flatten(node);
    }
    return result;
  },
  bindPickerChange3(e) {

    this.data.dataForm.receiveUnit = this.data.treeData[e.detail.value].value
    this.data.dataForm.field4 = this.data.treeData[e.detail.value].name
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  bindDateChange1: function (e) {
    this.data.dataForm.inValidStime = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  bindDateChange2: function (e) {
    this.data.dataForm.outValidEtime = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  bindPickerChange(e) {

    this.data.dataForm.carColor = this.data.colorList[e.detail.value].value
    this.data.dataForm.carColorName = this.data.colorList[e.detail.value].name
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  bindPickerChangeCar(e) {
    this.data.dataForm.carType = this.data.carList[e.detail.value].value
    this.data.dataForm.carTypeName = this.data.carList[e.detail.value].name
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  noInput13: function (e) {
    this.data.dataForm.userCode = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  noInput: function (e) {
    this.data.dataForm.inviteCode = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  carNoInput: function (e) {
    this.data.dataForm.carNo = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput2: function (e) {
    this.data.dataForm.forwardUnit = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput3: function (e) {
    this.data.dataForm.forwardPhone = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  // onInput4: function (e) {
  //   this.data.dataForm.field4 = e.detail.value
  //   this.setData({
  //     dataForm: this.data.dataForm
  //   })
  // },
  // onInput5: function (e) {
  //   this.data.dataForm.receivePhone = e.detail.value
  //   this.setData({
  //     dataForm: this.data.dataForm
  //   })
  // },
  onInput6: function (e) {
    this.data.dataForm.inFactTask = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput7: function (e) {
    this.data.dataForm.remark = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput8: function (e) {
    this.data.dataForm.carDriverName = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput9: function (e) {
    this.data.dataForm.carDriverPhone = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  file: function (e) {
    this.data.dataForm.file = e.detail.res
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  getAdd() {
    const {
      carNo,
      carColor,
      carType,
      forwardUnit,
      // forwardPhone,
      // field4,
      // receivePhone,
      inValidStime,
      outValidEtime,
      inFactTask,
      carDriverName,
      carDriverPhone,
      receiveUnit,
      userCode,
      inviteCode
      // persons
    } = this.data.dataForm

    if (!inviteCode.trim()) {
      wx.showToast({
        title: '邀请码不能为空',
        icon: 'none',
      });
      return;
    }
    if (!userCode.trim()) {
      wx.showToast({
        title: '被访人姓名不能为空',
        icon: 'none',
      });
      return;
    }
    if (!carNo.trim()) {
      wx.showToast({
        title: '车牌号不能为空',
        icon: 'none',
      });
      return;
    }
    if (!carColor.trim()) {
      wx.showToast({
        title: '车牌颜色不能为空',
        icon: 'none',
      });
      return;
    }
    if (!carType.trim()) {
      wx.showToast({
        title: '车辆类型不能为空',
        icon: 'none',
      });
      return;
    }
    if (!carDriverName.trim()) {
      wx.showToast({
        title: '司机姓名不能为空',
        icon: 'none',
      });
      return;
    }
    if (!carDriverPhone.trim()) {
      wx.showToast({
        title: '司机电话不能为空',
        icon: 'none',
      });
      return;
    }
    if (!forwardUnit.trim()) {
      wx.showToast({
        title: '来访单位不能为空',
        icon: 'none',
      });
      return;
    }
    // if (!forwardPhone.trim()) {
    //   wx.showToast({
    //     title: '来访单位联系方式不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }
    // if (!field4.trim()) {
    //   wx.showToast({
    //     title: '到访单位不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }
    // if (!receivePhone.trim()) {
    //   wx.showToast({
    //     title: '到访单位联系方式不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }
    if (!inValidStime.trim()) {
      wx.showToast({
        title: '有效期始不能为空',
        icon: 'none',
      });
      return;
    }
    if (!outValidEtime.trim()) {
      wx.showToast({
        title: '有效期止不能为空',
        icon: 'none',
      });
      return;
    }

    let time = isWithin24Hours(inValidStime, outValidEtime)
    if (!time) {
      wx.showToast({
        title: '日期区间不能超过24小时',
        icon: 'none',
      });
      return;
    }
    if (!inFactTask.trim()) {
      wx.showToast({
        title: '事由不能为空',
        icon: 'none',
      });
      return;
    }
    if (!receiveUnit.trim()) {
      wx.showToast({
        title: '到访部室不能为空',
        icon: 'none',
      });
      return;
    }
    // if (persons.length == 0) {
    //   wx.showToast({
    //     title: '随车人员不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }

    if (this.data.isSubmitting) return; // 如果正在提交，则不再处理新的提交请求
    // 设置为正在提交状态
    this.setData({
      isSubmitting: true
    });
    wx.showLoading({
      title: '提交中...',
    });
    api.carOrderAdd(this.data.dataForm).then(res => {
      setTimeout(function () {

        // this.setData({
        //   isSubmitting: false
        // }); // 请求成功后重置提交状态
        wx.hideLoading();
        wx.redirectTo({
          url: '/pages/busList/bus', //附带两个参数过去
        })
      }, 3000)
    }).catch(e => {
      this.setData({
        isSubmitting: false
      }); // 请求失败后重置提交状态
    })
  },
  showList() {
    wx.redirectTo({
      url: '/pages/busList/bus', //附带两个参数过去
    })
  },
  personsAdd() {
    this.data.dataForm.persons.push({
      personName: '',
      idCard: '',
      // sexC: '',
      telNum: '',
    })
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  personsDet: function (e) {
    const index = e.currentTarget.dataset.index;
    this.data.dataForm.persons.splice(index, 1);
    this.setData({
      dataForm: this.data.dataForm
    })

  },
  bindPickerChangeSex: function (e) {
    const index = e.currentTarget.dataset.index;
    const name = e.currentTarget.dataset.name;

    if (name == 'personName') {
      this.data.dataForm.persons[index].personName = e.detail.value;
    }
    if (name == 'idCard') {
      this.data.dataForm.persons[index].idCard = e.detail.value;
    }
    if (name == 'telNum') {
      this.data.dataForm.persons[index].telNum = e.detail.value;
    }
    // if (name == 'sexC') {
    //   this.data.dataForm.persons[index].sexC = this.data.sexList[e.detail.value].value;
    //   this.data.dataForm.persons[index].sexCName = this.data.sexList[e.detail.value].name;
    // }
    this.setData({
      dataForm: this.data.dataForm
    })

  },
})